﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UserStatuModel : MonoBehaviour {

    public UISprite avatar_id;
    public UITexture wxAvatar;
    public UILabel coinCount;
    //btn
    public GameObject LoadIPAndId;
    
    public GameObject Zhuang;
    public GameObject OKSprit;
    public GameObject Cover_TuoGuan;
    private Color baseColor;
    private int SeatID;
    private string userInfoPath = "GameMahjong/NextGameP/UserInfo";


    //聊天
    public GameObject textObj;
    public UILabel text;
    public UISprite face;
    EginMahjong mahjong = EginMahjong.Instance;
    EginUser user = EginUser.Instance;
    private void Awake()
    {
        baseColor = avatar_id.color;
        UIEventListener.Get(LoadIPAndId).onClick = ButtonClick;
    }
    void ButtonClick(GameObject button)
    {
        if (button.name == "Btn")
        {
            ClickEvent();
        }
    }
    void ClickEvent() {
        Debug.Log("玩家的seatid" + SeatID);
        GameObject go = LoadModelReturnObj(userInfoPath);
        go.GetComponent<UserInfoModel>().LoadIndex(SeatID);
    }
    void Start()
     {
        //LeaveStatu();
        //加载玩家信息不要写在Awake 写在start里面避免显不未加载

        LoadCoin();

        if (mahjong.OtherCanClick)
        {
            Zhuang.SetActive(true);
        }
       
        
        
    }
    public void LoadInfo(int index)
    {
        SeatID=index;
        LoadCoin();
    }
    public void LoadCoin() {
        for (int i = 0; i < user.gameUserInfoList.Count; i++) {
            if (SeatID == user.gameUserInfoList[i].SeatId) {
                if(user.gameUserInfoList[i].AvatarUrl != ""){
                    WXAvatarManager._instance.GetWxAvatar(user.gameUserInfoList[i].Uid,wxAvatar,user.gameUserInfoList[i].AvatarUrl);
                    wxAvatar.gameObject.SetActive(true);
                }
                else
                {
                    avatar_id.spriteName = user.gameUserInfoList[i].AvatarId.ToString();
                }
                
                //StartCoroutine(GetWWWFACE(wxAvatar, user.gameUserInfoList[i].AvatarUrl));
                int tempCoin = 0;
                if (user.ControlScene == 1)
                {
                    tempCoin = user.gameUserInfoList[i].Score;
                }
                else
                {
                    tempCoin = user.gameUserInfoList[i].BagCoinCount;
                }
                string str = user.ReturnMoney(tempCoin);
                coinCount.text = str;
            }
        }
        
    }

    /// <summary>
    /// 加载微信头像
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    //IEnumerator GetWWWFACE(UITexture texture, string url)
    //{

    //    if (url != "")
    //    {
    //        WWW www = new WWW(url);


    //        yield return www;
    //        if (www.isDone)
    //        {
    //            if (www.error == null) { }
    //            texture.mainTexture = www.texture;

    //        }
    //        else
    //        {

    //        }
    //        www.Dispose();
    //    }

    //}     
       
        
    

    GameObject LoadModelReturnObj(string TempPath)
    {
        Object TempFriend = Resources.Load(TempPath, typeof(GameObject));
        GameObject go = Instantiate(TempFriend, Vector3.zero, transform.rotation, this.transform) as GameObject;
        return go;
    }
    //更改离线状态
   public void LeaveStatu() {
        
        avatar_id.color = Color.black;
        OKSprit.SetActive(false);
    }
    //准备状态
    public void readyStatu() {
        avatar_id.color = baseColor;
        if (mahjong.roomStatus != ROOM_STATUS.Gaming) {
            OKSprit.SetActive(true);
        }
        
    }
    public void sitStatu() {
        avatar_id.color = baseColor;
    }
}
